package com.miracle.core.security.handle;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.miracle.common.utils.ServletUtils;
import com.miracle.system.base.domain.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.miracle.common.utils.StringUtils;
import com.miracle.core.security.domain.SecurityUserDetails;
import com.miracle.core.security.service.TokenService;

/**
 * 自定义退出处理类 返回成功
 *
 * @author miracle
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler
{
    @Autowired
    private TokenService tokenService;

    /**
     * 退出处理
     *
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException
    {
        SecurityUserDetails securityUserDetails = tokenService.getUserDetails(request);
        if (StringUtils.isNotNull(securityUserDetails))
        {
            tokenService.delUserDetails(securityUserDetails.getToken());
        }
        ServletUtils.renderString(response, JSON.toJSONString(ResultMap.error(HttpServletResponse.SC_OK,"退出成功")));
        //response.sendError(HttpServletResponse.SC_OK, "退出成功");


    }
}
